﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RadarSim
{
	/// <summary>
	/// MainWindow.xaml 的交互逻辑
	/// </summary>
	public partial class MainWindow : Window
	{
        /// <summary>
        /// 一级菜单Index
        /// </summary>
        public int menuindex;
        /// <summary>
        /// 二级菜单Index
        /// </summary>
        public int controlwheelindex;
        /// <summary>
        /// 当前显示的页面
        /// </summary>
        private PageBase currentPage;

		public MainWindow()
		{
			this.InitializeComponent();
            this.menu.SARCurrentSelectedIndex = 0;
            this.controlwheel.SARCurrentSelectedIndex = 1;
            controlwheelindex = 1;
            menuindex = 0;
            this.mainFrame.Source = new Uri("Pages/PdMain.xaml", UriKind.RelativeOrAbsolute);

			// 在此点下面插入创建对象所需的代码。
            this.mainFrame.ContentRendered += new EventHandler(mainFrame_ContentRendered);
        }


        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point pos = e.GetPosition(this);
                if (pos.Y < 100)
                {
                    this.DragMove();
                }                   
            }
        }

        private void maxminbtn_SARMaxClick(object sender, MouseButtonEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Maximized)
            {
                this.WindowState = System.Windows.WindowState.Normal;
            }
            else
            {
                this.WindowState = System.Windows.WindowState.Maximized;
            }
        }

        private void maxminbtn_SARMinClick(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        private void maxminbtn_SARCloseClick(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void menu_SARMenuClick(int menuItemIndex)
        {
            if (menuindex == menuItemIndex)
            {
                return;
            }
            this.controlwheel.SARCurrentSelectedIndex = 1;
            controlwheelindex = 1;

            if(menuItemIndex == 0)
            {
                this.mainFrame.Source = new Uri("Pages/PdMain.xaml", UriKind.RelativeOrAbsolute);
            }else if(menuItemIndex == 1)
            {
                this.mainFrame.Source = new Uri("Pages/SfMain.xaml", UriKind.RelativeOrAbsolute);
            }else if(menuItemIndex == 2)
            {
                this.mainFrame.Source = new Uri("Pages/SarMain.xaml", UriKind.RelativeOrAbsolute);
            }            
            menuindex = menuItemIndex;
        }

        private void controlwheel_SARControlWheelClick(int btnIndex)
        {
            if (controlwheelindex == btnIndex)
            {
                return;
            }
            string prefix = "Pd";
            if (this.menu.SARCurrentSelectedIndex == 0)
            {
                prefix = "Pd";
            }
            else if (this.menu.SARCurrentSelectedIndex == 1)
            {
                prefix = "Sf";
            }
            else if (this.menu.SARCurrentSelectedIndex == 2)
            {
                prefix = "Sar";
            }            

            controlwheelDispatch(btnIndex, prefix);

            controlwheelindex = btnIndex;
        }

        void controlwheelDispatch(int btnIndex, string prefix)
        {
            string classname = currentPage.GetType().ToString();
            string classname1 = "RadarSim." + prefix + "Main";
            
            if (btnIndex == 1)
            {

                if (classname.Equals(classname1))
                {
                    currentPage.ControlArgsClick();
                }
                else
                {
                    this.mainFrame.Source = new Uri("Pages/" + prefix + "Main.xaml", UriKind.RelativeOrAbsolute);
                }

            }
            else if (btnIndex == 2)
            {
                this.mainFrame.Source = new Uri("Pages/AppConfig.xaml", UriKind.RelativeOrAbsolute);
            }
            else if (btnIndex == 0)
            {

                if (classname.Equals(classname1))
                {
                    currentPage.RealTimeInfoClick();
                }
                else
                {
                    this.mainFrame.Source = new Uri("Pages/" + prefix + "Main.xaml", UriKind.RelativeOrAbsolute);
                }

            }
            else if (btnIndex == 3)
            {
                this.mainFrame.Source = new Uri("Pages/"+prefix+"InitArgs.xaml", UriKind.RelativeOrAbsolute);
            }
        }

        void mainFrame_ContentRendered(object sender, EventArgs e)
        {
            currentPage = this.mainFrame.Content as PageBase;
            if (controlwheelindex == 0)
            {
                currentPage.RealTimeInfoClick();
            }else if (controlwheelindex == 1)
            {
                currentPage.ControlArgsClick();
            }else if (controlwheelindex == 2)
            {
                currentPage.AppConfigClick();
            }
            else 
            {
                currentPage.InitArgsClick();
            }          
        }
       
	}
}